package com.yanqu.road.dao.impl.monopolymarket;

import com.alibaba.fastjson.JSON;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.monopolymarket.office.MonopolyMarketOfficeTip;
import com.yanqu.road.entity.monopolymarket.MonopolyMarketUserData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description
 * @Author cwq
 * @Data 2021/8/13 16:08
 */
public class MonopolyMarketUserDataDaoImpl extends TempDataObjectDao<MonopolyMarketUserData> {
    public static String getMonopolyMarketUserDataSql = "select * from `t_u_monopolymarket_user_data` where userId = ?";
    public MonopolyMarketUserData getMonopolyMarketUserData(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getMonopolyMarketUserDataSql);
            int i = 1;
            call.setLong(i++, userId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateMonopolyMarketUserDataSql = "update `t_u_monopolymarket_user_data` set physical=?,physicalRefreshTime=?,officeCurPos=?,officeLevel=?,officeNo=?,officeSeatTime=?,officeRobTimes=?,dailyOfficeRefreshTime=?,dailyResGrabRefreshTime=?,hasTip=?,teaLastNum=?,teaLastGetTime=?,enterOfficeReportLastTime=?,enterResGrabReportLastTime=?,officeTip=? where userId=?";
    @Override
    protected boolean updateData(MonopolyMarketUserData monopolyMarketUserData) {
        monopolyMarketUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketUserData.getPhysical()));
        para.put(i++, new DbParameter(Types.BIGINT, monopolyMarketUserData.getPhysicalRefreshTime()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketUserData.getOfficeCurPos()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketUserData.getOfficeLevel()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketUserData.getOfficeNo()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketUserData.getOfficeSeatTime()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketUserData.getOfficeRobTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketUserData.getDailyOfficeRefreshTime()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketUserData.getDailyResGrabRefreshTime()));
        para.put(i++, new DbParameter(Types.BOOLEAN, monopolyMarketUserData.getHasTip()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketUserData.getTeaLastNum()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketUserData.getTeaLastGetTime()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketUserData.getEnterOfficeReportLastTime()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketUserData.getEnterResGrabReportLastTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(monopolyMarketUserData.getOfficeTip())));
        para.put(i++, new DbParameter(Types.BIGINT, monopolyMarketUserData.getUserId()));
        if (getDb().executeNoneQuery(updateMonopolyMarketUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertMonopolyMarketUserDataSql = "insert into `t_u_monopolymarket_user_data`(userId,physical,physicalRefreshTime,officeCurPos,officeLevel,officeNo,officeSeatTime,officeRobTimes,dailyOfficeRefreshTime,dailyResGrabRefreshTime,hasTip,teaLastNum,teaLastGetTime,enterOfficeReportLastTime,enterResGrabReportLastTime,officeTip) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(MonopolyMarketUserData monopolyMarketUserData) {
        monopolyMarketUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, monopolyMarketUserData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketUserData.getPhysical()));
        para.put(i++, new DbParameter(Types.BIGINT, monopolyMarketUserData.getPhysicalRefreshTime()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketUserData.getOfficeCurPos()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketUserData.getOfficeLevel()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketUserData.getOfficeNo()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketUserData.getOfficeSeatTime()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketUserData.getOfficeRobTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketUserData.getDailyOfficeRefreshTime()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketUserData.getDailyResGrabRefreshTime()));
        para.put(i++, new DbParameter(Types.BOOLEAN, monopolyMarketUserData.getHasTip()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketUserData.getTeaLastNum()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketUserData.getTeaLastGetTime()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketUserData.getEnterOfficeReportLastTime()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketUserData.getEnterResGrabReportLastTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(monopolyMarketUserData.getOfficeTip())));
        if (getDb().executeNoneQuery(insertMonopolyMarketUserDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected MonopolyMarketUserData parseData(ResultSet rs) throws SQLException {
        MonopolyMarketUserData monopolyMarketUserData = new MonopolyMarketUserData();
        monopolyMarketUserData.setUserId(rs.getLong("userId"));
        monopolyMarketUserData.setPhysical(rs.getInt("physical"));
        monopolyMarketUserData.setPhysicalRefreshTime(rs.getLong("physicalRefreshTime"));
        monopolyMarketUserData.setOfficeCurPos(rs.getInt("officeCurPos"));
        monopolyMarketUserData.setOfficeLevel(rs.getInt("officeLevel"));
        monopolyMarketUserData.setOfficeNo(rs.getInt("officeNo"));
        monopolyMarketUserData.setOfficeSeatTime(rs.getInt("officeSeatTime"));
        monopolyMarketUserData.setOfficeRobTimes(rs.getInt("officeRobTimes"));
        monopolyMarketUserData.setDailyOfficeRefreshTime(rs.getInt("dailyOfficeRefreshTime"));
        monopolyMarketUserData.setDailyResGrabRefreshTime(rs.getInt("dailyResGrabRefreshTime"));
        monopolyMarketUserData.setHasTip(rs.getBoolean("hasTip"));
        monopolyMarketUserData.setTeaLastNum(rs.getInt("teaLastNum"));
        monopolyMarketUserData.setTeaLastGetTime(rs.getInt("teaLastGetTime"));
        monopolyMarketUserData.setEnterOfficeReportLastTime(rs.getInt("enterOfficeReportLastTime"));
        monopolyMarketUserData.setEnterResGrabReportLastTime(rs.getInt("enterResGrabReportLastTime"));
        monopolyMarketUserData.setOfficeTip(JSON.parseObject(rs.getString("officeTip"), MonopolyMarketOfficeTip.class));

        return monopolyMarketUserData;
    }

}
